<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="Rust的Option的正确打开方式"><meta name="keywords" content="Rust"><meta name="author" content="LiYang"><meta name="copyright" content="LiYang"><title>Rust的Option的正确打开方式 | 一条鲤鱼</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="一条鲤鱼" type="application/atom+xml">
</head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="Toggle article">Toggle site</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">Catalog</div><div class="sidebar-toc__progress"><span class="progress-notice">You've read</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">1.</span> <span class="toc-text">使用场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%AF%E7%94%A8%E6%93%8D%E4%BD%9C"><span class="toc-number">2.</span> <span class="toc-text">可用操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#match"><span class="toc-number">2.1.</span> <span class="toc-text">match</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#expect"><span class="toc-number">2.2.</span> <span class="toc-text">expect()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#unwrap"><span class="toc-number">2.3.</span> <span class="toc-text">unwrap()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#unwrap-or"><span class="toc-number">2.4.</span> <span class="toc-text">unwrap_or()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#unwrap-or-else"><span class="toc-number">2.5.</span> <span class="toc-text">unwrap_or_else()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map"><span class="toc-number">2.6.</span> <span class="toc-text">map()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map-or"><span class="toc-number">2.7.</span> <span class="toc-text">map_or()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map-or-else"><span class="toc-number">2.8.</span> <span class="toc-text">map_or_else()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ok-or"><span class="toc-number">2.9.</span> <span class="toc-text">ok_or()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ok-or-else"><span class="toc-number">2.10.</span> <span class="toc-text">ok_or_else()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#iter"><span class="toc-number">2.11.</span> <span class="toc-text">iter()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#and"><span class="toc-number">2.12.</span> <span class="toc-text">and()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#and-then"><span class="toc-number">2.13.</span> <span class="toc-text">and_then()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#filter"><span class="toc-number">2.14.</span> <span class="toc-text">filter()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#or"><span class="toc-number">2.15.</span> <span class="toc-text">or()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#or-else"><span class="toc-number">2.16.</span> <span class="toc-text">or_else()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#take"><span class="toc-number">2.17.</span> <span class="toc-text">take()</span></a></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">LiYang</div><div class="author-info__description text-center"></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">Articles</span><span class="pull-right">13</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">Tags</span><span class="pull-right">6</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">Categories</span><span class="pull-right">7</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">一条鲤鱼</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"></span></div><div id="post-info"><div id="post-title">Rust的Option的正确打开方式</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2021-09-22</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>本文参考：<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/ce5bddf4b335">https://www.jianshu.com/p/ce5bddf4b335</a></p>
<hr>
<p>Rust基于强大的类型，泛型系统，采用Option枚举的方式，在程序运行过程中表达“有”和“无”的概念。</p>
<p>回顾一下，Option是一个枚举类型：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="class"><span class="keyword">enum</span> <span class="title">Option</span></span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="literal">None</span>,</span><br><span class="line">    <span class="literal">Some</span>(T),</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>官方文档请看：<a target="_blank" rel="noopener" href="https://links.jianshu.com/go?to=https://doc.rust-lang.org/std/option/enum.Option.html">std::option::Option</a></p>
<h2 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h2><p>根据<code>std::option</code>的介绍，<code>Option&lt;T&gt;</code>有以下7种用法：</p>
<p>1、初始化值</p>
<p>2、作为在整个输入范围没有定义的函数的返回值</p>
<p>3、如果函数可能有错误，可用 <code>Option&lt;T&gt;</code>作为返回值，用<code>None</code>代表可能出现的错误。</p>
<p>4、用作<code>struct</code>的可选字段</p>
<p>5、用作函数的可选参数</p>
<p>6、空指针</p>
<p>7、用作复杂情况的返回值</p>
<p><strong>初始化值</strong><br>如果一个变量可能有值，也可能没有，那么可以使用Option<T>初始化该变量。</T></p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> s = <span class="literal">Some</span>(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">if</span> <span class="keyword">let</span> <span class="literal">Some</span>(<span class="keyword">ref</span> s) = s&#123;</span><br><span class="line">  <span class="built_in">println!</span>(<span class="string">&quot;&#123;:?&#125;&quot;</span>,*s);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>有或没有值所造成的影响，只有在使用该变量时才体现出来，否则只是<code>println!()</code>没必要初始化一个<code>Option</code>,能使用一个<code>Option&lt;T&gt;</code>的情况有几种，分别是：</p>
<ul>
<li>作为函数返回值</li>
<li><code>struct</code>有带有<code>Option&lt;T&gt;</code>的字段时，初始化该字段需要创建<code>Option&lt;T&gt;</code></li>
<li>作为函数参数</li>
</ul>
<p><strong>空指针</strong><br>此处搬运标准库的例子<br>Rust的指针指向一定是个合法的内存地址，因此Rust没有空指针，取而代之的是可选指针(optional pointer)，比如<code>Option&lt;Box&lt;T&gt;&gt;</code>。<br> 下面的例子使用<code>Option</code>创建一个<code>Option&lt;Box&lt;i32&gt;&gt;</code>，为了使用i32的值，<code>check_option</code>函数使用了模式匹配，判断box里是否有值，因为Option也可能没值嘛。</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> optional = <span class="literal">None</span>;</span><br><span class="line">check_optional(optional);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> optional = <span class="literal">Some</span>(<span class="built_in">Box</span>::new(<span class="number">9000</span>));</span><br><span class="line">check_optional(optional);</span><br><span class="line"><span class="comment">//接收Option作为参数</span></span><br><span class="line"><span class="function"><span class="keyword">fn</span> <span class="title">check_optional</span></span>(optional: <span class="built_in">Option</span>&lt;<span class="built_in">Box</span>&lt;<span class="built_in">i32</span>&gt;&gt;) &#123;</span><br><span class="line">    <span class="keyword">match</span> optional &#123;</span><br><span class="line">        <span class="literal">Some</span>(<span class="keyword">ref</span> p) =&gt; <span class="built_in">println!</span>(<span class="string">&quot;has value &#123;&#125;&quot;</span>, p),</span><br><span class="line">        <span class="literal">None</span> =&gt; <span class="built_in">println!</span>(<span class="string">&quot;has no value&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的例子使用Option创建一个安全的可空指针(nullable pointer)，这种Option的用法在Rust中很常见，因此Rust对此做了优化，确保<code>Option&lt;Box&lt;T&gt;&gt;</code>像一个很普通的指针那样高效率，且不带额外的开销。</p>
<p><strong>复杂情况的返回值</strong><br> Rust是个静态强类型的语言，特性<code>trait</code>是编译期采用静态分发的方式为每个struct添加方法，因此Rust的抽象没有额外的开销。<br> 当我们调用某个复杂的库，实现其一个带有类型占位符的<code>trait</code>时，我们要指定一个类型，否则编译不过，但是此类型不是随便指定的，它可能需要实现某种<code>trait</code>，因为后续的操作可能要用到某种<code>trait</code>的方法，这时库会给我们提供一些实现某种<code>trait</code>的基础类型，比如<code>Option&lt;T&gt;</code>,<code>Result&lt;T,E&gt;</code>等。</p>
<h2 id="可用操作"><a href="#可用操作" class="headerlink" title="可用操作"></a>可用操作</h2><ol>
<li>match</li>
<li>if let</li>
<li>unwrap()等组合器</li>
</ol>
<h3 id="match"><a href="#match" class="headerlink" title="match"></a>match</h3><p>match是个很强大的模式匹配，很适合匹配Option等枚举类型。</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">fn</span> <span class="title">main</span></span>()&#123;</span><br><span class="line">    <span class="keyword">let</span> d = Student::new(<span class="string">&quot;xiaoming&quot;</span>.to_string(),<span class="number">18</span>);</span><br><span class="line">    <span class="keyword">let</span> d = <span class="literal">Some</span>(d);</span><br><span class="line">   <span class="keyword">match</span> d&#123;</span><br><span class="line">    <span class="literal">Some</span>(s)=&gt;&#123;</span><br><span class="line">      <span class="built_in">println!</span>(<span class="string">&quot;&#123;:?&#125;&quot;</span>, s.name);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="literal">None</span>=&gt;&#123;</span><br><span class="line">      <span class="built_in">println!</span>(<span class="string">&quot;None&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Option的一些基本操作，相信大家一看就懂，这里不再赘述，下面重点看下Option的组合器。这种写法属于函数式编程，刚接触的人肯定对<code>map</code>,   <code>map_or</code>,<code>map_or_else</code>很困惑。<br> 所谓的函数式是一种编程范式，Rust有函数式编程的支持，不代表Rust是函数式编程语言。<br> 想了解函数式编程?<br> <a target="_blank" rel="noopener" href="https://links.jianshu.com/go?to=https://baike.baidu.com/item/%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/4035031?fr=aladdin">函数式编程–百度百科</a><br> 下面以说明，源码，例子的顺序，一一介绍这些好玩的东东：<br> <strong>注意：断言<code>assert_eq!(a,b)</code>，如果a==b，什么都不发生，否则中断程序。</strong></p>
<h3 id="expect"><a href="#expect" class="headerlink" title="expect()"></a>expect()</h3><ul>
<li>说明：<br> 有值，返回值，否则，中断程序，打印msg错误信息。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">expect</span></span>(<span class="keyword">self</span>, msg: &amp;<span class="built_in">str</span>) -&gt; T &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(val) =&gt; val,</span><br><span class="line">        <span class="literal">None</span> =&gt; expect_failed(msg),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="string">&quot;value&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.expect(<span class="string">&quot;the world is ending&quot;</span>), <span class="string">&quot;value&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line">x.expect(<span class="string">&quot;the world is ending&quot;</span>); <span class="comment">// panics with `the world is ending`</span></span><br></pre></td></tr></table></figure>

<h3 id="unwrap"><a href="#unwrap" class="headerlink" title="unwrap()"></a>unwrap()</h3><ul>
<li><p>说明：<br>有值，返回值，否则，中断程序。</p>
</li>
<li><p>源码：</p>
</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">unwrap</span></span>(<span class="keyword">self</span>) -&gt; T &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(val) =&gt; val,</span><br><span class="line">        <span class="literal">None</span> =&gt; <span class="built_in">panic!</span>(<span class="string">&quot;called `Option::unwrap()` on a `None` value&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="string">&quot;air&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.unwrap(), <span class="string">&quot;air&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.unwrap(), <span class="string">&quot;air&quot;</span>); <span class="comment">// fails</span></span><br></pre></td></tr></table></figure>

<h3 id="unwrap-or"><a href="#unwrap-or" class="headerlink" title="unwrap_or()"></a>unwrap_or()</h3><ul>
<li>说明：<br> 有值，返回值，否则返回一个默认值。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">unwrap_or</span></span>(<span class="keyword">self</span>, def: T) -&gt; T &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(x) =&gt; x,</span><br><span class="line">        <span class="literal">None</span> =&gt; def,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="string">&quot;car&quot;</span>).unwrap_or(<span class="string">&quot;bike&quot;</span>), <span class="string">&quot;car&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">None</span>.unwrap_or(<span class="string">&quot;bike&quot;</span>), <span class="string">&quot;bike&quot;</span>);</span><br></pre></td></tr></table></figure>

<h3 id="unwrap-or-else"><a href="#unwrap-or-else" class="headerlink" title="unwrap_or_else()"></a>unwrap_or_else()</h3><ul>
<li>说明：<br> 有值，返回值，否则，执行闭包。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">unwrap_or_else</span></span>&lt;F: <span class="built_in">FnOnce</span>() -&gt; T&gt;(<span class="keyword">self</span>, f: F) -&gt; T &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(x) =&gt; x,</span><br><span class="line">        <span class="literal">None</span> =&gt; f(),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> k = <span class="number">10</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="number">4</span>).unwrap_or_else(|| <span class="number">2</span> * k), <span class="number">4</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">None</span>.unwrap_or_else(|| <span class="number">2</span> * k), <span class="number">20</span>);</span><br></pre></td></tr></table></figure>

<h3 id="map"><a href="#map" class="headerlink" title="map()"></a>map()</h3><ul>
<li>说明：<br> 改变值，并返回另一个Option。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">map</span></span>&lt;U, F: <span class="built_in">FnOnce</span>(T) -&gt; U&gt;(<span class="keyword">self</span>, f: F) -&gt; <span class="built_in">Option</span>&lt;U&gt; &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(x) =&gt; <span class="literal">Some</span>(f(x)),</span><br><span class="line">        <span class="literal">None</span> =&gt; <span class="literal">None</span>,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> maybe_some_string = <span class="literal">Some</span>(<span class="built_in">String</span>::from(<span class="string">&quot;Hello, World!&quot;</span>));</span><br><span class="line"><span class="comment">// `Option::map` takes self *by value*, consuming `maybe_some_string`</span></span><br><span class="line"><span class="keyword">let</span> maybe_some_len = maybe_some_string.map(|s| s.len());</span><br><span class="line"></span><br><span class="line"><span class="built_in">assert_eq!</span>(maybe_some_len, <span class="literal">Some</span>(<span class="number">13</span>));</span><br></pre></td></tr></table></figure>

<h3 id="map-or"><a href="#map-or" class="headerlink" title="map_or()"></a>map_or()</h3><ul>
<li>说明：<br> 有值，则执行闭包返回值，否则返回一个自定义的默认值。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">map_or</span></span>&lt;U, F: <span class="built_in">FnOnce</span>(T) -&gt; U&gt;(<span class="keyword">self</span>, default: U, f: F) -&gt; U &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(t) =&gt; f(t),</span><br><span class="line">        <span class="literal">None</span> =&gt; default,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="string">&quot;foo&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.map_or(<span class="number">42</span>, |v| v.len()), <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.map_or(<span class="number">42</span>, |v| v.len()), <span class="number">42</span>);</span><br></pre></td></tr></table></figure>

<h3 id="map-or-else"><a href="#map-or-else" class="headerlink" title="map_or_else()"></a>map_or_else()</h3><ul>
<li>说明：<br> 有值，执行闭包，否则执行另一个闭包。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">map_or_else</span></span>&lt;U, D: <span class="built_in">FnOnce</span>() -&gt; U, F: <span class="built_in">FnOnce</span>(T) -&gt; U&gt;(<span class="keyword">self</span>, default: D, f: F) -&gt; U &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(t) =&gt; f(t),</span><br><span class="line">        <span class="literal">None</span> =&gt; default(),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> k = <span class="number">21</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="string">&quot;foo&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.map_or_else(|| <span class="number">2</span> * k, |v| v.len()), <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.map_or_else(|| <span class="number">2</span> * k, |v| v.len()), <span class="number">42</span>);</span><br></pre></td></tr></table></figure>

<h3 id="ok-or"><a href="#ok-or" class="headerlink" title="ok_or()"></a>ok_or()</h3><ul>
<li>说明：<br> 有值，返回Result，否则返回自定义的错误。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">ok_or</span></span>&lt;E&gt;(<span class="keyword">self</span>, err: E) -&gt; <span class="built_in">Result</span>&lt;T, E&gt; &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(v) =&gt; <span class="literal">Ok</span>(v),</span><br><span class="line">        <span class="literal">None</span> =&gt; <span class="literal">Err</span>(err),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="string">&quot;foo&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.ok_or(<span class="number">0</span>), <span class="literal">Ok</span>(<span class="string">&quot;foo&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.ok_or(<span class="number">0</span>), <span class="literal">Err</span>(<span class="number">0</span>));</span><br></pre></td></tr></table></figure>

<h3 id="ok-or-else"><a href="#ok-or-else" class="headerlink" title="ok_or_else()"></a>ok_or_else()</h3><ul>
<li>说明：<br> 有值，返回Result,否则执行代表错误的闭包。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">ok_or_else</span></span>&lt;E, F: <span class="built_in">FnOnce</span>() -&gt; E&gt;(<span class="keyword">self</span>, err: F) -&gt; <span class="built_in">Result</span>&lt;T, E&gt; &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(v) =&gt; <span class="literal">Ok</span>(v),</span><br><span class="line">        <span class="literal">None</span> =&gt; <span class="literal">Err</span>(err()),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="string">&quot;foo&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.ok_or_else(|| <span class="number">0</span>), <span class="literal">Ok</span>(<span class="string">&quot;foo&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.ok_or_else(|| <span class="number">0</span>), <span class="literal">Err</span>(<span class="number">0</span>));</span><br></pre></td></tr></table></figure>

<h3 id="iter"><a href="#iter" class="headerlink" title="iter()"></a>iter()</h3><ul>
<li>说明：<br> 把Option转换为迭代器。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">iter</span></span>(&amp;<span class="keyword">self</span>) -&gt; Iter&lt;T&gt; &#123;</span><br><span class="line">    Iter &#123; inner: Item &#123; opt: <span class="keyword">self</span>.as_ref() &#125; &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="number">4</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.iter().next(), <span class="literal">Some</span>(&amp;<span class="number">4</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;<span class="built_in">u32</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.iter().next(), <span class="literal">None</span>);</span><br></pre></td></tr></table></figure>

<h3 id="and"><a href="#and" class="headerlink" title="and()"></a>and()</h3><ul>
<li>说明：<br> 有值，返回另一Option，否则返回None。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">and</span></span>&lt;U&gt;(<span class="keyword">self</span>, optb: <span class="built_in">Option</span>&lt;U&gt;) -&gt; <span class="built_in">Option</span>&lt;U&gt; &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(_) =&gt; optb,</span><br><span class="line">        <span class="literal">None</span> =&gt; <span class="literal">None</span>,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">let</span> y: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.and(y), <span class="literal">None</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;<span class="built_in">u32</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="keyword">let</span> y = <span class="literal">Some</span>(<span class="string">&quot;foo&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.and(y), <span class="literal">None</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">let</span> y = <span class="literal">Some</span>(<span class="string">&quot;foo&quot;</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.and(y), <span class="literal">Some</span>(<span class="string">&quot;foo&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;<span class="built_in">u32</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="keyword">let</span> y: <span class="built_in">Option</span>&lt;&amp;<span class="built_in">str</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.and(y), <span class="literal">None</span>);</span><br></pre></td></tr></table></figure>

<h3 id="and-then"><a href="#and-then" class="headerlink" title="and_then()"></a>and_then()</h3><ul>
<li>说明：<br> 有值，执行闭包，否则返回None。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">and_then</span></span>&lt;U, F: <span class="built_in">FnOnce</span>(T) -&gt; <span class="built_in">Option</span>&lt;U&gt;&gt;(<span class="keyword">self</span>, f: F) -&gt; <span class="built_in">Option</span>&lt;U&gt; &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(x) =&gt; f(x),</span><br><span class="line">        <span class="literal">None</span> =&gt; <span class="literal">None</span>,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">fn</span> <span class="title">sq</span></span>(x: <span class="built_in">u32</span>) -&gt; <span class="built_in">Option</span>&lt;<span class="built_in">u32</span>&gt; &#123; <span class="literal">Some</span>(x * x) &#125;</span><br><span class="line"><span class="function"><span class="keyword">fn</span> <span class="title">nope</span></span>(_: <span class="built_in">u32</span>) -&gt; <span class="built_in">Option</span>&lt;<span class="built_in">u32</span>&gt; &#123; <span class="literal">None</span> &#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="number">2</span>).and_then(sq).and_then(sq), <span class="literal">Some</span>(<span class="number">16</span>));</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="number">2</span>).and_then(sq).and_then(nope), <span class="literal">None</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="number">2</span>).and_then(nope).and_then(sq), <span class="literal">None</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">None</span>.and_then(sq).and_then(sq), <span class="literal">None</span>);</span><br></pre></td></tr></table></figure>

<h3 id="filter"><a href="#filter" class="headerlink" title="filter()"></a>filter()</h3><ul>
<li>说明：<br> 过滤器，过滤出自己想要的值。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">filter</span></span>&lt;P: <span class="built_in">FnOnce</span>(&amp;T) -&gt; <span class="built_in">bool</span>&gt;(<span class="keyword">self</span>, predicate: P) -&gt; <span class="keyword">Self</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">let</span> <span class="literal">Some</span>(x) = <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> predicate(&amp;x) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">Some</span>(x)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="literal">None</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">fn</span> <span class="title">is_even</span></span>(n: &amp;<span class="built_in">i32</span>) -&gt; <span class="built_in">bool</span> &#123;</span><br><span class="line">    n % <span class="number">2</span> == <span class="number">0</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">None</span>.filter(is_even), <span class="literal">None</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="number">3</span>).filter(is_even), <span class="literal">None</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="number">4</span>).filter(is_even), <span class="literal">Some</span>(<span class="number">4</span>));</span><br></pre></td></tr></table></figure>

<h3 id="or"><a href="#or" class="headerlink" title="or()"></a>or()</h3><ul>
<li>说明：<br> 有值，返回自身，否则返回自定义的Option。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">or</span></span>(<span class="keyword">self</span>, optb: <span class="built_in">Option</span>&lt;T&gt;) -&gt; <span class="built_in">Option</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(_) =&gt; <span class="keyword">self</span>,</span><br><span class="line">        <span class="literal">None</span> =&gt; optb,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">let</span> y = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.or(y), <span class="literal">Some</span>(<span class="number">2</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x = <span class="literal">None</span>;</span><br><span class="line"><span class="keyword">let</span> y = <span class="literal">Some</span>(<span class="number">100</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.or(y), <span class="literal">Some</span>(<span class="number">100</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x = <span class="literal">Some</span>(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">let</span> y = <span class="literal">Some</span>(<span class="number">100</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.or(y), <span class="literal">Some</span>(<span class="number">2</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> x: <span class="built_in">Option</span>&lt;<span class="built_in">u32</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="keyword">let</span> y = <span class="literal">None</span>;</span><br><span class="line"><span class="built_in">assert_eq!</span>(x.or(y), <span class="literal">None</span>);</span><br></pre></td></tr></table></figure>

<h3 id="or-else"><a href="#or-else" class="headerlink" title="or_else()"></a>or_else()</h3><ul>
<li>说明：<br> 有值，返回自身，否则执行闭包。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">or_else</span></span>&lt;F: <span class="built_in">FnOnce</span>() -&gt; <span class="built_in">Option</span>&lt;T&gt;&gt;(<span class="keyword">self</span>, f: F) -&gt; <span class="built_in">Option</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">match</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="literal">Some</span>(_) =&gt; <span class="keyword">self</span>,</span><br><span class="line">        <span class="literal">None</span> =&gt; f(),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>栗子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">fn</span> <span class="title">nobody</span></span>() -&gt; <span class="built_in">Option</span>&lt;&amp;<span class="symbol">&#x27;static</span> <span class="built_in">str</span>&gt; &#123; <span class="literal">None</span> &#125;</span><br><span class="line"><span class="function"><span class="keyword">fn</span> <span class="title">vikings</span></span>() -&gt; <span class="built_in">Option</span>&lt;&amp;<span class="symbol">&#x27;static</span> <span class="built_in">str</span>&gt; &#123; <span class="literal">Some</span>(<span class="string">&quot;vikings&quot;</span>) &#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">Some</span>(<span class="string">&quot;barbarians&quot;</span>).or_else(vikings), <span class="literal">Some</span>(<span class="string">&quot;barbarians&quot;</span>));</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">None</span>.or_else(vikings), <span class="literal">Some</span>(<span class="string">&quot;vikings&quot;</span>));</span><br><span class="line"><span class="built_in">assert_eq!</span>(<span class="literal">None</span>.or_else(nobody), <span class="literal">None</span>);</span><br></pre></td></tr></table></figure>

<h3 id="take"><a href="#take" class="headerlink" title="take()"></a>take()</h3><ul>
<li>说明：<br> 取出一个值。</li>
<li>源码：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="function"><span class="keyword">fn</span> <span class="title">take</span></span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>) -&gt; <span class="built_in">Option</span>&lt;T&gt; &#123;</span><br><span class="line">    mem::replace(<span class="keyword">self</span>, <span class="literal">None</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>例子：</li>
</ul>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="keyword">mut</span> x = <span class="literal">Some</span>(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">let</span> y = x.take();</span><br><span class="line"><span class="built_in">assert_eq!</span>(x, <span class="literal">None</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(y, <span class="literal">Some</span>(<span class="number">2</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="keyword">mut</span> x: <span class="built_in">Option</span>&lt;<span class="built_in">u32</span>&gt; = <span class="literal">None</span>;</span><br><span class="line"><span class="keyword">let</span> y = x.take();</span><br><span class="line"><span class="built_in">assert_eq!</span>(x, <span class="literal">None</span>);</span><br><span class="line"><span class="built_in">assert_eq!</span>(y, <span class="literal">None</span>);</span><br></pre></td></tr></table></figure>

</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">LiYang</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://example.com/2021/09/22/Rust的Option的正确打开方式/">http://example.com/2021/09/22/Rust的Option的正确打开方式/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Rust/">Rust</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2021/10/09/C-emplace-back/"><i class="fa fa-chevron-left">  </i><span>C++_emplace_back</span></a></div><div class="next-post pull-right"><a href="/2021/09/22/%E7%90%86%E8%A7%A3Rust%E7%9A%84%E5%BC%95%E7%94%A8%E5%92%8C%E5%80%9F%E7%94%A8/"><span>理解Rust的引用和借用</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2013 - 2022 By LiYang</div><div class="framework-info"><span>Driven - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>Theme - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script></body></html>